package com.jt.blog.service;

import com.jt.blog.model.Blog;
import com.jt.blog.model.BlogStatistics;
import com.jt.blog.mapper.BlogStatisticsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

/**
 * @author : 戴瑞
 * @Description :博客服务层的读操作
 * @create : 2017-05-25 18:20
 **/

@Service
@Transactional(propagation = Propagation.REQUIRED)
public class BlogWriteServiceImpl extends AbstractBaseService<Blog> implements BlogWriteService {
    @Autowired
    BlogStatisticsMapper blogStatisticsMapper;

    public void create(Blog blog) {
        mapper.insert(blog);
        BlogStatistics blogStatistics = new BlogStatistics(blog.getId());
        blogStatisticsMapper.insert(blogStatistics);
    }

    @Override
    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    public void delete(Long[] ids) {
        for (Long id : ids) {
            mapper.deleteByPrimaryKey(id);
        }
    }

    public void update(Blog blog) {
        mapper.updateByPrimaryKeySelective(blog);
    }

    /**
     * 更新 文章的状态，系统管理员进行操作的
     * 0 公开  1私有 3 管理员隐藏
     * @param blogs
     */
    @Override
    public void updateStatus(Blog[] blogs) {
        for (Blog blog : blogs) {
            // 只更新博文状态
            if (blog.getPersonal()==0){
                blog.setPersonal(3);
            }else if (blog.getPersonal()==3){
                blog.setPersonal(0);
            }
            mapper.updateByPrimaryKeySelective(blog);
        }
    }


}
